package com.ipet.order.controller;

import com.github.pagehelper.PageInfo;
import com.ipet.order.pojo.Order;
import com.ipet.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api("订单模块")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @ApiOperation(value = "分页查询所有订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码" , name = "pageNo" , required = false , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "每页的数据数" , name = "pageSize" , required = false , dataType = "Integer" , paramType = "query")
    })
    @GetMapping("/orders")
    public ResponseEntity<PageInfo<Order>> allOrderPage(@RequestParam(defaultValue = "1") Integer pageNo ,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Order> allOrder = orderService.findAllOrder(pageNo, pageSize);
        System.out.println(allOrder);
        if(allOrder == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(allOrder);
    }
    @ApiOperation(value = "删除订单")
    @ApiImplicitParam(value = "要删除的订单id" , name = "orderId" , required = true , dataType = "Long" , paramType = "query")
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> deleteOrderById(Long orderId){
        boolean remove = orderService.removeById(orderId);
        return ResponseEntity.ok(remove);
    }
    @ApiOperation(value = "根据编号查询订单")
    @ApiImplicitParam(value = "订单编号" , name = "orderId" , required = true , dataType = "Long"  , paramType = "query")
    @GetMapping("/getOrderById")
    public ResponseEntity<PageInfo<Order>> getOrderById(Long orderId , @RequestParam(defaultValue = "1") Integer pageNo ,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Order> oneById = orderService.findOneById(orderId, pageNo, pageSize);
        if(oneById == null){
           return  ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(oneById);
    }
}
